//
//  HMHomeViewController.m
//  HMPersonalDoctor
//
//  Created by GD on 16/9/7.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMHomeViewController.h"
#import "CityChooseViewController.h"
#import "WeatherView.h"
#import "HMHospitalCollectionView.h"
#import "ZFBMineLayout.h"
#import "HMHomeSubViewController.h"
#import "HMSubJsonViewController.h"
#import "HMSideslipViewController.h"

@interface HMHomeViewController ()<HMHospitalCollectionViewDelegate>

@property (nonatomic,weak) HMHospitalCollectionView *hostpitalView;

@property (nonatomic,weak) WeatherView *weatherView;

@property (nonatomic,weak) UIScrollView *scrollView;

@end

@implementation HMHomeViewController

//-(void)loadView{//可否将首页根控制器的根视图改为ScrollView?
//
//    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    
//    self.view = scrollView;
//}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupNav];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    scrollView.backgroundColor = [UIColor orangeColor];
//    scrollView.contentSize = CGSizeMake(0, 800);
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    self.scrollView.contentSize = [UIScreen mainScreen].bounds.size;
    self.scrollView.alwaysBounceVertical = true;
//    self.scrollView.alwaysBounceHorizontal = false;
    self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 59, 0);
    
    [self setupWeatherView];
    
    [self setUpHostpitalView];
}

-(void)setupNav{
    
    HMButton *cityBtn = [[HMButton alloc] init];
    [cityBtn setTitle:@"城市" forState:UIControlStateNormal];
    [cityBtn setImage:[UIImage imageNamed:@"下拉.png"] forState:UIControlStateNormal];
    [cityBtn addTarget:self action:@selector(pullDown:) forControlEvents:UIControlEventTouchUpInside];
    [cityBtn sizeToFit];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:cityBtn];
    self.navigationItem.rightBarButtonItem = rightItem;

    
}



-(void)setupWeatherView{
    
    WeatherView *weatherView =  [[[NSBundle mainBundle] loadNibNamed:@"WeatherView" owner:nil options:nil] firstObject];;
    
    [self.scrollView addSubview:weatherView];
    self.weatherView = weatherView;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    [weatherView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(0);
        make.height.offset(150);
        make.width.mas_equalTo(width);
    }];
    
    [weatherView getWeatherDataWithCityName:@"北京"];

}



//下拉
-(void)pullDown:(UIBarButtonItem *)cityItem{
    
    CityChooseViewController *vc = [[CityChooseViewController alloc]init];
    [vc returnCityInfo:^(NSString *province, NSString *area) {
        cityItem.title = area;
        [self.weatherView getWeatherDataWithCityName:area];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)setUpHostpitalView{
    ZFBMineLayout *layout = [[ZFBMineLayout alloc]init];
    HMHospitalCollectionView *hostpitalView = [[HMHospitalCollectionView alloc] initWithFrame:CGRectMake(0, 150, self.view.bounds.size.width, 550) collectionViewLayout:layout];
    NSLog(@"hostpitalView.frame=====%@",NSStringFromCGRect(hostpitalView.frame));
    hostpitalView.subDelegate =self;
    [self.scrollView addSubview:hostpitalView];
    
    self.hostpitalView = hostpitalView;
    
    [self testHospital];
}

-(void)hospitalCollectionView:(HospitalCollectionView *)hospitalCollectionView didSelectIndexPath:(NSIndexPath *)indexPath{

    HMHomeSubViewController *subVc = [[HMHomeSubViewController alloc] init];
    if (indexPath.section == 0 && indexPath.item == 0) {//跳转找科室
        subVc.url = @"http://3g.xywy.com/so/?channel=zongheSo&f_s=expert&fromurl=xywy_app";
        subVc.subTitle = @"找科室";
    }else if (indexPath.section == 0 && indexPath.item == 1){//医院
        subVc.url = @"http://3g.zhuanjia.xywy.com/hospital-top-list.html";
        subVc.subTitle = @"找医院";
    }else if (indexPath.section == 1 && indexPath.item == 0){//转诊
        subVc.url = @"http://3g.zhuanjia.xywy.com/nplus/index.htm";
        subVc.subTitle = @"预约转诊";
    }else if (indexPath.section == 2 && indexPath.item == 0){//同仁堂
        subVc.url = @"http://3g.zhuanjia.xywy.com/so/kszhuanjia-179-7-109-0-doc.html&amp;fromurl=xywy_app";
        subVc.subTitle = @"重点推荐";
    }else if (indexPath.section == 2 && indexPath.item == 1){//广州医科大学
        subVc.url = @"http://3g.zhuanjia.xywy.com/so/kszhuanjia-386-2-51-3-doc.html&amp;fromurl=xywy_app";
        subVc.subTitle = @"重点推荐";
    }else if (indexPath.section == 2 && indexPath.item == 2){//北京协和医院
        HMSubJsonViewController *subVc = [[HMSubJsonViewController alloc] init];
        subVc.url = @"http://3g.zhuanjia.xywy.com/index.php?/zhuanjia/zhuanjia/ajax_doc/&hosid=2&departid1=2&departid2=50&province=1&page=1&callback=returnMsg&_=1473507180064";
        subVc.subTitle = @"重点推荐";
        [self.navigationController pushViewController:subVc animated:YES];
        
        return;
    }else if (indexPath.section == 2 && indexPath.item == 3){//上海红房子医院
        subVc.url = @"http://3g.zhuanjia.xywy.com/so/kszhuanjia-167-3-59-2-doc.html&amp;fromurl=xywy_app";
        subVc.subTitle = @"重点推荐";
    }else if (indexPath.section == 2 && indexPath.item == 4){//北京积水潭医院
        HMSubJsonViewController *subVc = [[HMSubJsonViewController alloc] init];
        
        subVc.url = @"http://3g.zhuanjia.xywy.com/index.php?/zhuanjia/zhuanjia/ajax_doc/&hosid=45&departid1=1&departid2=29&province=0&page=1&callback=returnMsg&_=1473507461451";
        subVc.subTitle = @"重点推荐";
        
        [self.navigationController pushViewController:subVc animated:YES];
        
        return;
    }else if (indexPath.section == 2 && indexPath.item == 5){//北京301 医院
        subVc.url = @"http://3g.zhuanjia.xywy.com/so/kszhuanjia-45-1-29-0-doc.html&amp;fromurl=xywy_app";
        subVc.subTitle = @"重点推荐";
    }
    
    [self.navigationController pushViewController:subVc animated:YES];




}

-(void)testHospital{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [manager GET:@"http://api.wws.xywy.com/api.php/xywyapp/gohospital/index?api=1076&os=ios&pro=xywyf32l24WmcqquqqTdhXaEng&sign=9b3bca2428bb6452b76296b3e3365da8&source=xywyapp&token=1989779e0e96b007e96099d5a479639f&version=1.1" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@   %@",[responseObject class],responseObject);
        
        NSLog(@"_oneSingleArray---%@",responseObject[@"data"][@"doctorservice"][@"data"]);
        NSLog(@"_twoInOneLieArray==%@",responseObject[@"data"][@"referral"][@"data"]);
        NSMutableArray *tempArray3 = [NSMutableArray array];
        [responseObject[@"data"][@"doctorservice"][@"data"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ZFBMineOptionModel *model = [[ZFBMineOptionModel alloc] init];
            
            [model setValuesForKeysWithDictionary:obj];
            //
            [tempArray3 addObject:model];
            //            [_oneSingleArray addObject:model];
        }];//_oneSingleArray;
        //        _oneSingleArray = tempArray3;
        
        self.hostpitalView.oneSingleArray = tempArray3;
        
        NSMutableArray *tempArray2 = [NSMutableArray array];
        id obj = responseObject[@"data"][@"hotdapart"][@"data"];
        NSLog(@"%@",obj);
        [responseObject[@"data"][@"hotdapart"][@"data"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ZFBMineOptionModel *model = [[ZFBMineOptionModel alloc] init];
            
            [model setValuesForKeysWithDictionary:obj];
            
            [tempArray2 addObject:model];
        }];//NSArray *_bottemArray;
        self.hostpitalView.bottemArray = tempArray2.copy;
        NSMutableArray *tempArray = [NSMutableArray array];
        [responseObject[@"data"][@"referral"][@"data"]enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            ZFBMineOptionModel *model = [[ZFBMineOptionModel alloc] init];
            
            [model setValuesForKeysWithDictionary:obj];
            
            [tempArray addObject:model];
            
            
        }];//_twoInOneLieArray;
        self.hostpitalView.twoInOneLieArray = tempArray.copy;
        
        [self.hostpitalView reloadData];
        
        //        [responseObject writeToFile:@"/Users/geduo/Desktop/hospital.json" atomically:YES];///Users/geduo/Desktop/yuebanxiaoyequ%20tanyonglin.mp3
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}





@end



@implementation HMButton

- (void)layoutSubviews {
    // 一定不要忘记调用super
    [super layoutSubviews];
    
    // label的x变为 0
        CGRect titleLabelRect = self.titleLabel.frame;
        titleLabelRect.origin.x = 0;
        self.titleLabel.frame = titleLabelRect;
    
//    self.titleLabel.x = 0;
    
    // imageView的x变为label的宽
        CGRect imageViewRect = self.imageView.frame;
        imageViewRect.origin.x = titleLabelRect.size.width + 10;
        self.imageView.frame = imageViewRect;
    
//    self.imageView.x = self.titleLabel.w;
}

@end

